package org.grpc.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
import org.grpc.annotation.Monitor;
import org.grpc.userInfo.*;
import org.grpc.util.HttpRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

/**
 * description:
 *
 * @author shun.chen
 * @date 2022/5/10
 */
@Slf4j
@GrpcService
public class UserInfoServiceGrpcImpl extends UserInfoServiceGrpc.UserInfoServiceImplBase {

    @Override
    public void queryUserInfo(UserInfoReq request, io.grpc.stub.StreamObserver<UserInfoResponse> responseObserver) {
        UserInfoResponse.Builder userInfoResp = UserInfoResponse.newBuilder();
        userInfoResp.setCode(0).setMsg("success").setSuccess(true);
        UserInfo.Builder userInfo = UserInfo.newBuilder();
        userInfo.setId(request.getId());
        userInfo.setName(request.getName());

        userInfoResp.setData(UserInfoResponse.Data.newBuilder().setUserInfo(userInfo));
        responseObserver.onNext(userInfoResp.build());
        responseObserver.onCompleted();
    }

    @Override
    public void queryUserInfo2(UserInfoReq request, io.grpc.stub.StreamObserver<UserInfoResponse> responseObserver) {
        super.queryUserInfo2(request, responseObserver);
    }

    @Monitor(description = "/queryUserInfo3")
    @Override
    public void queryUserInfo3(UserStr request, io.grpc.stub.StreamObserver<UserStr> responseObserver) {
        System.out.println("queryUserInfo3 =======> " + request.getStr());
        String url = "http://localhost:8891/userInfo/callBack";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "吕布");
        HashMap map = new HashMap();

        map.put("token", "java");
        String result = HttpRequestUtils.postJson(url, jsonObject.toJSONString(), null);
        log.info("queryUserInfo3 /userInfo/callBack : {}",result);
        responseObserver.onNext(UserStr.newBuilder().setStr("msg : success").build());
        responseObserver.onCompleted();

    }
}
